/**
 * 
 */
package com.cqcis.uip.base.logadaptor.impl;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.spi.LoggingEvent;

import com.cqcis.uip.base.logadaptor.AppenderAdapterable;

/**
 * @author huangmy
 * @date 2008-2-23
 */
public class DailyRollingFileAppenderAdapter extends DailyRollingFileAppender implements AppenderAdapterable {
	private Object lock = new Object();
	private File file = null;
	
	/**
	 * The default constructor does nothing.
	 */
	public DailyRollingFileAppenderAdapter() {
	}

	/**
	 * 指定参数构造
	 * 
	 */
	public DailyRollingFileAppenderAdapter(Layout layout, String filename,
			String datePattern) throws IOException {
		super(layout, filename, datePattern);
	}
		
	@Override
	public void append(LoggingEvent event) {
		synchronized (this) {
			if (file == null) {
				file = new File(getFile());
			}
			
			if (!file.exists()) { // 不存在, 可能被删除了
				file.getParentFile().mkdirs();//  重建父目录
				activateOptions();							
				file = new File(getFile());
			}
		}
							
		super.append(event);
	}
	
	/* ------ 修改原方法行为 ------- */
	
	@Override
	public synchronized void doAppend(LoggingEvent event) {
		synchronized (lock) {
			super.doAppend(event);
		}
	}
}
